package com.test.demo01_triangle;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class Renderer02 implements GLSurfaceView.Renderer {

    private final String TAG = "Renderer02";

    private Context context;

    private FloatBuffer mVertexFloatBuffer;

    private final String VERTEX_SHADER_FILE = "triangle_vertex_shader_2.glsl";
    private final String FRAGMENT_SHADER_FILE = "triangle_fragment_shader_2.glsl";
    private final String A_POSITION = "aPosition";
    private final String A_COLOR = "aColor";

    //在数组中，一个顶点需要3个来描述其位置，需要3个偏移量
    private final int COORDINATES_PER_VERTEX = 3;
    // 一个顶点需要3个值来描述颜色信息的偏移量,从测试结果来看，透明通道不算在颜色通道里面
    private final int COORDINATES_PER_COLOR = 3;

    //每个Float,4个字节
    public final int BYTES_PER_FLOAT = 4;

    //在数组中，描述一个顶点，总共的顶点需要的偏移量。这里因为只有位置顶点，所以和上面的值一样
    private final int TOTAL_COMPONENT_COUNT = COORDINATES_PER_VERTEX + COORDINATES_PER_COLOR;
    //一个点需要的byte偏移量。
    private final int STRIDE = TOTAL_COMPONENT_COUNT * BYTES_PER_FLOAT;

    //顶点的坐标系
    private float TRIANGLE_COORDINATES[] = {
            //Order of coordinates: X, Y, Z, R, G, B, A
            0.0f, 0.5f, 0.0f, 1.f, 0f, 0f, // top
            -0.5f, -0.5f, 0.0f, 0.f, 1f, 0f,  // bottom left
            0.5f, -0.5f, 0.0f, 0.f, 0f, 1f, // bottom right
    };

    private final int VERTEX_COUNT = TRIANGLE_COORDINATES.length / TOTAL_COMPONENT_COUNT;
    //program的指针
    private int mProgramObjectId;


    public Renderer02(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        mVertexFloatBuffer = ByteBuffer
                .allocateDirect(TRIANGLE_COORDINATES.length * BYTES_PER_FLOAT)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer()
                .put(TRIANGLE_COORDINATES);
        mVertexFloatBuffer.position(0);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        String vertexCode = OpenGLUtil.readAssetShaderCode(context, VERTEX_SHADER_FILE);
        String fragmentCode = OpenGLUtil.readAssetShaderCode(context, FRAGMENT_SHADER_FILE);
        int vertexId = OpenGLUtil.compileShaderCode(GLES20.GL_VERTEX_SHADER, vertexCode);
        int fragmentId = OpenGLUtil.compileShaderCode(GLES20.GL_FRAGMENT_SHADER, fragmentCode);

        mProgramObjectId = GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgramObjectId, vertexId);
        GLES20.glAttachShader(mProgramObjectId, fragmentId);

        LogUtil.d(TAG, "onSurfaceCreated() -- 1111");
        GLES20.glLinkProgram(mProgramObjectId);
        GLES20.glUseProgram(mProgramObjectId);
        LogUtil.d(TAG, "onSurfaceCreated() -- 222");
        int aPosition = GLES20.glGetAttribLocation(mProgramObjectId, A_POSITION);
        mVertexFloatBuffer.position(0);
        GLES20.glVertexAttribPointer(
                aPosition,
                COORDINATES_PER_VERTEX, //告诉他用几个偏移量来描述一个顶点
                GLES20.GL_FLOAT,
                false,
                STRIDE,//一个顶点需要多少个字节的偏移量
                mVertexFloatBuffer
        );
        //2.开始启用我们的position
        GLES20.glEnableVertexAttribArray(aPosition);

        LogUtil.d(TAG, "onSurfaceCreated() -- 33333333");
        int aColor = GLES20.glGetAttribLocation(mProgramObjectId, A_COLOR);
        mVertexFloatBuffer.position(COORDINATES_PER_VERTEX);
        GLES20.glVertexAttribPointer(
                aColor,
                COORDINATES_PER_COLOR,
                GLES20.GL_FLOAT,
                false,
                STRIDE,
                mVertexFloatBuffer
        );
        GLES20.glEnableVertexAttribArray(aColor);
        LogUtil.d(TAG, "onSurfaceCreated() -- 44444444");
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, VERTEX_COUNT);
    }
}
